package main;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import dao.CommDAO;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.awt.event.ActionEvent;

public class StayIn extends JFrame {

	private CommDAO dao = new CommDAO();
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel label_4;
	private JLabel label_5;
	private JLabel label_6;
	private JLabel label_7;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JLabel room_id;
	private JLabel room_num;
	private JLabel floor;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					StayIn frame = new StayIn();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public StayIn() {
		setTitle("录入");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 521, 388);
		contentPane = new JPanel();
		contentPane.setToolTipText("");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("设备id");
		label.setBounds(48, 34, 54, 15);
		contentPane.add(label);
		
		textField = new JTextField();
		textField.setBounds(100, 31, 86, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel label_1 = new JLabel("门牌号");
		label_1.setBounds(206, 34, 54, 15);
		contentPane.add(label_1);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(248, 31, 86, 21);
		contentPane.add(textField_1);
		
		JButton btnNewButton = new JButton("查询");
		btnNewButton.setBounds(355, 30, 68, 23);
		contentPane.add(btnNewButton);
		
		label_2 = new JLabel("设备id");
		label_2.setBounds(134, 85, 54, 15);
		contentPane.add(label_2);
		
		label_3 = new JLabel("门牌号");
		label_3.setBounds(134, 114, 54, 15);
		contentPane.add(label_3);
		
		label_4 = new JLabel("存放楼层");
		label_4.setBounds(134, 139, 54, 15);
		contentPane.add(label_4);
		
		label_5 = new JLabel("身份证号码");
		label_5.setBounds(134, 164, 68, 15);
		contentPane.add(label_5);
		
		label_6 = new JLabel("姓名");
		label_6.setBounds(134, 189, 54, 15);
		contentPane.add(label_6);
		
		label_7 = new JLabel("录入时间");
		label_7.setBounds(134, 214, 54, 15);
		contentPane.add(label_7);
		
		textField_2 = new JTextField();
		textField_2.setBounds(206, 161, 86, 21);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(206, 186, 86, 21);
		contentPane.add(textField_3);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(206, 211, 86, 21);
		contentPane.add(textField_4);
		
		room_id = new JLabel("");
		room_id.setForeground(Color.RED);
		room_id.setBounds(206, 85, 86, 15);
		contentPane.add(room_id);
		
		room_num = new JLabel("");
		room_num.setForeground(Color.RED);
		room_num.setBounds(206, 114, 86, 15);
		contentPane.add(room_num);
		
		floor = new JLabel("");
		floor.setForeground(Color.RED);
		floor.setBounds(206, 139, 86, 15);
		contentPane.add(floor);
		
		btnNewButton_1 = new JButton("登记录入");
		btnNewButton_1.setEnabled(false);
		btnNewButton_1.setBounds(169, 260, 91, 23);
		contentPane.add(btnNewButton_1);
		
		btnNewButton_2 = new JButton("关闭");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnNewButton_2.setBounds(266, 260, 68, 23);
		contentPane.add(btnNewButton_2);
		

		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnNewButton_1.setEnabled(false);
				String sql = "select * from room where id = '" + textField.getText() + "' or num = '" + textField_1.getText() + "'";
				System.out.println(sql);
				List<Map<String,Object>> select = dao.select(sql);
				if(select.size() > 0) {
					
					if(select.get(0).get("state").toString().equals("未录入")){
						btnNewButton_1.setEnabled(true);
						room_id.setText(select.get(0).get("id").toString());
						room_num.setText(select.get(0).get("num").toString());
						floor.setText(select.get(0).get("floor").toString());
					}
				}
			}
		});
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					String sql = "insert into record values('" 
							+ UUID.randomUUID().toString().replace("-", "").substring(0, 6) + "', '"
							+ textField_2.getText()  + "', '"
							+ textField_3.getText()  + "', '"
							+ room_num.getText()  + "', '"
							+ floor.getText()  + "', '"
							+ textField_4.getText()  + "', '"
							+ "')";
					String sql1 = "update room set state = '已录入' where id = '" + room_id.getText() + "'";
					System.out.println(sql);
					System.out.println(sql1);
					dao.commOper(sql);
					dao.commOper(sql1);
			}
		});
	}

}
